package com.sq.util.page;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sq.util.StringUtil;
import com.sq.util.web.initer.InitIgnore;
import com.sq.util.web.initer.LimitLower;

public class PageQuery {

    public static final int PAGE_SIZE = 10;

    /**
     * 当前页
     */
    @LimitLower(lower = 1)
    private int page;

    @JsonIgnore
    @InitIgnore
    private Map<String, Object> queryParams = new HashMap<String, Object>();

    @JsonIgnore
    @InitIgnore
    private String orderBy = "";

    /**
     * 每页记录数
     */
    private int size = PAGE_SIZE;

    public PageQuery() {
        super();
    }

    public PageQuery(int page) {
        super();
        this.page = page;
    }

    @JsonIgnore
    public boolean containsQueryParam(String key) {
        return this.queryParams.containsKey(key);
    }

    @JsonIgnore
    public void addPlusQueryParam(String key, Integer plus) {
        if (plus == null) {
            return;
        }
        if (plus < 1) {
            return;
        }
        this.queryParams.put(key, plus);
    }

    @JsonIgnore
    public void addPlusQueryParam(String key, Long plus) {
        if (plus == null) {
            return;
        }
        if (plus < 1) {
            return;
        }
        this.queryParams.put(key, plus);
    }

    @JsonIgnore
    public void addQueryParam(String key, Integer value) {
        if (value == null) {
            return;
        }
        this.queryParams.put(key, value);
    }

    @JsonIgnore
    public void addQueryParam(String key, Long value) {
        if (value == null) {
            return;
        }
        this.queryParams.put(key, value);
    }

    @JsonIgnore
    public void addQueryParam(String key, BigDecimal value) {
        if (value == null) {
            return;
        }
        this.queryParams.put(key, value);
    }

    @JsonIgnore
    public void addQueryParam(String key, String value) {
        if (StringUtil.isBlank(value)) {
            return;
        }
        this.queryParams.put(key, value);
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public Map<String, Object> getQueryParams() {
        return queryParams;
    }

    public void setQueryParams(Map<String, Object> queryParams) {
        this.queryParams = queryParams;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "PageQuery [page=" + page + ", queryParams=" + queryParams + ", orderBy=" + orderBy + ", size=" + size
                + "]";
    }

}
